var CN_COMMON_ARTICLE_DETAIL_COMMENTS = '';

var CN_COMMON_ARTICLE_DETAIL = function(){
	this.init();
};
CN_COMMON_ARTICLE_DETAIL.prototype.init = function(){
	var self = this;
	self.expand();
	
	self.initSomeThing();
	self.bandClick();
	self.bandResize();
}
/**
 * 拓展方法
 */
CN_COMMON_ARTICLE_DETAIL.prototype.expand = function(){
	var self = this;
	self.initSomeThing = function() {
		$(".smilies-icon").hover(function() {
			$(".smilies-box").show();
		}, function() {
			$(".smilies-box").hide();
		});
		$(".smilies-box").hover(function() {
			$(".smilies-box").show();
		}, function() {
			$(".smilies-box").hide();
		});
		
		$("#reply-user").click(function() {
			$(this).html("");
			$("#commentId").val("");
		});
	};
	self.bandClick = function() {
		
	};
	self.bandResize = function() {
	};
};
//供外部调用的方法
CN_COMMON_ARTICLE_DETAIL.prototype.readyReply = function(commentId, userName) {
	$("#reply-user").html("REPLY:" + userName);
	$("#comment").focus();
	$("#commentId").val(commentId);
};
CN_COMMON_ARTICLE_DETAIL.prototype.showReply = function(self, commentId) {
	if($(self).next(".children").length == 0) {
		
		var loadingIndex = submitHolder();
		jsonAjax('front/article/comment/reply/' + commentId + '.html', 'json', '', function(data) {
			closeSubmitHolder(loadingIndex);
			if(data.status == SysConstant.SUCCESS) {
				console.log(data.object);
				if(data.object) {
					cnCommonArticleDetail.traceComments(data.object);
					$(self).after(CN_COMMON_ARTICLE_DETAIL_COMMENTS);
				}
			} else {
				tipsFailed(data.msg, 1500);
			} 
		});
	} else {
		$(self).next(".children").remove();
	}
};

CN_COMMON_ARTICLE_DETAIL.prototype.traceComments = function(comments) {
	CN_COMMON_ARTICLE_DETAIL_COMMENTS = '';
	if(comments == null || comments.length == 0) {
        return;
    }
	CN_COMMON_ARTICLE_DETAIL_COMMENTS += '<ol class="children">';
    for (var i = 0; i < comments.length; i++) {
    	var comment = comments[i];
    	var childComments = comment.childList;
    	CN_COMMON_ARTICLE_DETAIL_COMMENTS += '<li class="comment byuser comment-author-jsd even thread-even depth-1 parent" id="comment-' + comment.id + '">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '    <div class="comment-body">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '        <div class="comment-author vcard">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '            <a target="_blank" title="前往 ' + comment.createUserName + ' 的主页" href="' + ctx + 'front/user/detail/' + comment.createUserId + '.html">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '                <img src="' + comment.userSmallIcon + '" class="avatar" alt="' + comment.createUserName + '" width="44" height="44">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '            </a>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '        </div>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '        <div class="comment-main">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '            <p>' + comment.commentContent + '</p>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '            <div class="comment-meta">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '                <span class="author"><cite>' + comment.createUserName + '</cite></span>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '                <span class="date"><recdate:format th:time="' + comment.createTime + '" /></span>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '                <span class="reply">';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '                    <a rel="nofollow" class="comment-reply-link" href="javascript:;" onclick="cnCommonArticleDetail.readyReply(\'' + comment.id + '\',\'' + comment.createUserName + '\')" >回复</a>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '                </span>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '            </div>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '        </div>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '        <a class="comment-floor" onclick="cnCommonArticleDetail.showReply(this, \'' + comment.id + '\')">查看全部回复（' + comment.commentNum + '）</a>';
		CN_COMMON_ARTICLE_DETAIL_COMMENTS += '	       <!-- .children -->';
		CN_COMMON_ARTICLE_DETAIL_COMMENTS += '    </div>';
	    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '</li>';
    }
    CN_COMMON_ARTICLE_DETAIL_COMMENTS += '</ol>';
}
CN_COMMON_ARTICLE_DETAIL.prototype.reply = function() {
	var commentAddFormIndex = submitHolder();
	if(!validInput("#comment", ['requried'], "请填写回复内容！")) {
		closeSubmitHolder(commentAddFormIndex);
		return;
	}
	$("#commentform").attr("action", ctx + "front/article/comment.html");
	$("#commentform").submit();
};
$(function() {
	window.cnCommonArticleDetail = new CN_COMMON_ARTICLE_DETAIL();
});